﻿using System.Modules.ExceptionHanlding;
using System.Modules.Extensions;
using System.Modules.SharePoint.Constants;
using System.Modules.SharePoint.Contracts.DTO;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;

namespace System.Modules.SharePoint.Extensions
{
    /// <summary>
    /// Publishing Web Extensions
    /// </summary>
    public static class PublishingWebExtensions
    {
        #region Fix Page Layout Field Url
        /// <summary>
        /// This method loops Across web pages and fixes the PublishingPageLayout item property.
        /// </summary>
        /// <param name="web"></param>
        public static void FixPageLayoutFieldUrl(this PublishingWeb web)
        {
            if (web != null)
            {
                if (web.PagesList != null)
                {
                    foreach (SPListItem pageItem in web.PagesList.Items)
                    {
                        SPFile pPageFile = pageItem.File;
                        if (pPageFile != null)
                        {
                            if (pPageFile.CheckOutType == SPFile.SPCheckOutType.None)
                            {
                                pPageFile.CheckOut();
                            }
                            try
                            {
                                var publishingPage = pageItem.LoadItem<PublishingPageItem>();
                                if (publishingPage.PublishingPageLayout != null && publishingPage.PublishingPageLayout.HasValue)
                                {
                                    string url = publishingPage.PublishingPageLayout.Value.Url;
                                    if (url.IsNotNullOrEmpty())
                                    {
                                        var relativeUrl = new Uri(url, UriKind.RelativeOrAbsolute);
                                        var newUrl = new SPFieldUrlValue {Description = publishingPage.PublishingPageLayout.Value.Description, Url = relativeUrl.AbsolutePath};
                                        pageItem[PublishingPageFieldNames.PUBLISHING_PAGE_LAYOUT] = newUrl;
                                        pageItem.SystemUpdate();
                                    }
                                }
                            }
                            catch (Exception exception)
                            {
                                exception.Handle();
                            }
                            if (pPageFile.CheckOutType != SPFile.SPCheckOutType.None)
                            {
                                pPageFile.CheckIn(string.Empty);
                            }
                            if (web.PagesList.EnableMinorVersions)
                            {
                                pPageFile.Publish("Published automatically");
                            }
                        }
                    }
                }
            }
        }
        #endregion
    }
}